package com.platform.common.xss;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * XSS过滤
 *
 * @author 肖亮
 */
public class XssFilter implements Filter {
	private String[] excludedPageArray = new String[] { "/service/model/" };

	@Override
	public void init(FilterConfig config) {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper((HttpServletRequest) request);

		boolean isExcludedPage = false;
		// 判断是否在过滤url之外
		for (String page : excludedPageArray) {
			if (((HttpServletRequest) request).getServletPath().indexOf(page) > -1) {
				isExcludedPage = true;
				break;
			}
		}
		// 排除过滤url
		if (isExcludedPage) {
			chain.doFilter(request, response);
		} else {
			chain.doFilter(xssRequest, response);
		}
	}

	@Override
	public void destroy() {
	}

}